﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using Commonlib;

namespace AlbonWeb.Order
{
    public partial class ItemSize : System.Web.UI.Page
    {
        private readonly BLL.ItemClothBLL itemclothbll = new BLL.ItemClothBLL();
        private readonly BLL.OrderItemBLL orderitembll = new BLL.OrderItemBLL();
        private readonly BLL.ItemBLL itembll = new BLL.ItemBLL();
        private readonly BLL.OrderBLL orderbll = new BLL.OrderBLL();
        private string itemid =HttpContext.Current.Request["itemid"].ToString();
        private readonly string username = HttpContext.Current.User.Identity.Name;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 
                if (Request["itemid"] != null)
                {
                    itemName.Text = itembll.getItemName(itemid);
                    string stylelist = itembll.getStyleList(itemid);
                    string cmdstr = "select * from Style where active=1 and ID in (" + stylelist + ")";
                    bindRep(cmdstr, repItem);
                }
                if (Request["orderitemid"] != null)
                {
                    ItemSize_btnNext.Text = "Update";
                    initData(Request["orderitemid"].ToString());
                }
                if (Request["copyitemid"] != null)
                {
                    initData(Request["copyitemid"].ToString());
                }
            }
        }
        //绑定Repeater数据
        protected void bindRep(string cmdstr, Repeater rep)
        {
            SqlHelper.BindRepeater(rep, SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null);
        }
        

        //绑定尺寸信息
        protected void repItem_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemIndex != -1)
            {
                string styleid = ((HiddenField)e.Item.FindControl("styleid")).Value;
                Repeater repSize = (Repeater)e.Item.FindControl("repSize");
                string cmdstr = "select * from SizeTempleteView where active=1 and styleid=" + styleid;
                bindRep(cmdstr, repSize);
            }
        }

        //加载要修改的数据
        protected void initData(string orderitemid)
        {
            string body="";
            string finished = "";
            BLL.ItemSizeBLL itemsizebll = new BLL.ItemSizeBLL();
            DataTable dt=itemsizebll.getItemSize(orderitemid);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                body += dt.Rows[i]["sizeTempleteID"].ToString() + "=" + dt.Rows[i]["bodysize"].ToString() + "$";
                finished += dt.Rows[i]["sizeTempleteID"].ToString() + "=" + dt.Rows[i]["finishedsize"].ToString() + "$";
            }
            if (body != "")
            {
                body = body.Substring(0, body.Length - 1);
                hfbody.Value = body;
            }
            if (finished != "")
            {
                finished = finished.Substring(0, finished.Length - 1);
                hffinish.Value = finished;
            }
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            //创建新的临时订单项(包括复制订单项)
            if (Request["orderitemid"] == null)
            {
                //添加新的订单项
                Albon.Model.OrderItem orderitem = new Albon.Model.OrderItem();
                orderitem.orderID =int.Parse(Session["orderid"].ToString());
                orderitem.itemID = int.Parse(itemid);
                orderitem.styleList = itembll.getStyleList(itemid);
                int orderitemid = orderitembll.addOrderItem(orderitem);

                addSize(orderitemid);
              
                Albon.Model.ItemCloth itemcloth = new Albon.Model.ItemCloth();
                
                itemcloth.orderItemID = orderitemid;
                //将主布料需求信息添加到ItemCloth中
                itemcloth.clothSize = (decimal)12.125;
                itemcloth.styleID = 0;
                itemcloth.Stylename = itemName.Text;
                itemclothbll.addItemCloth(itemcloth);
                if (Request["copyitemid"] != null)
                {
                    //如果是复制订单，则将copyitemid传递到样式页
                    Response.Redirect("/Order/ItemStyle.aspx?itemid=" + itemid + "&orderitemid=" + itemcloth.orderItemID+"&copyitemid="+Request["copyitemid"].ToString());
                }
                else
                {
                    Response.Redirect("/Order/ItemStyle.aspx?itemid=" + itemid + "&orderitemid=" + itemcloth.orderItemID);
                }
            }
            //修改订单项
            else if (Request["orderitemid"] != null)
            { 
                //更新ItemSize表
                addSize(int.Parse(Request["orderitemid"].ToString()));
                //更新clothSize值
                Albon.Model.ItemCloth itemcloth = new Albon.Model.ItemCloth();
                itemcloth.orderItemID=int.Parse(Request["orderitemid"].ToString());
                itemcloth.Stylename = itemName.Text;
                itemcloth.clothSize = (decimal)00.00;
                itemclothbll.updateItemSize(itemcloth);
                Response.Redirect("/Order/OrderItemList.aspx?orderid=" + Session["orderid"].ToString());
            }
        }

        protected void addSize(int orderitemid)
        {
            //在ItemSize表中添加信息
            Hashtable htbody = new Hashtable();
            Hashtable htfinish = new Hashtable();
            if (hfbody.Value != "")
            {
                string[] bodyflow = hfbody.Value.Split('$');

                for (int i = 0; i < bodyflow.Length; i++)
                {
                    string[] values = bodyflow[i].Split('=');
                    htbody.Add(values[0], values[1] == "" ? "0" : values[1]);
                }
            }
            if (hffinish.Value != "")
            {
                string[] finishflow = hffinish.Value.Split('$');
                for (int i = 0; i < finishflow.Length; i++)
                {
                    string[] values = finishflow[i].Split('=');
                    htfinish.Add(values[0], values[1] == "" ? "0" : values[1]);
                }
            }
            Albon.Model.ItemSize itemsize = new Albon.Model.ItemSize();
            itemsize.orderItemID = orderitemid;
            BLL.SizeTempleteBLL sizetempletebll = new BLL.SizeTempleteBLL();
            DataTable dt = sizetempletebll.getTempleteByID(itemid);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string sizetempleteid = dt.Rows[i]["ID"].ToString();
                itemsize.sizeTempleteID = int.Parse(sizetempleteid);
                if (htbody.Contains(sizetempleteid))
                {
                    itemsize.bodySize = decimal.Parse(htbody[sizetempleteid].ToString());
                    itemsize.finishedSize = decimal.Parse(htfinish[sizetempleteid].ToString());
                }
                BLL.ItemSizeBLL itemsizebll = new BLL.ItemSizeBLL();
                itemsizebll.addItemSize(itemsize);
            }
        }
    }
}